/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*********************************************************************
 *
 * Main pthread task.
 *                 
 * Author Cory Dixon
 *********************************************************************/

// Global Defines
#define _POSIX_C_SOURCE 199506L
                    
// standard headers
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
 
#include <iostream>
#include <string>

using namespace std;

// include project include here
#include "Thread.h"

#undef _POSIX_TIMERS

// global variable area
ShmStruct ShmStructVar;

int main()
{

	sigset_t waitSigs;
	sigfillset(&waitSigs);
	sigdelset(&waitSigs,SIGINT);
	sigdelset(&waitSigs,SIGKILL);
	sigprocmask(SIG_BLOCK,&waitSigs,NULL);

	//---------------------------------------------------------- 
	// threads
	//---------------------------------------------------------- 
	pThread t1("t1");
	pThread t2("t2");

	//---------------------------------------------------------- 
	// init the shared memory area
	//---------------------------------------------------------- 
	pthread_mutex_init(&ShmStructVar.mtx, NULL);

	//---------------------------------------------------------- 
	// init the different threads
	//---------------------------------------------------------- 
	cout << "Initializing threads ... " << endl;
	if ( t1.init(&ShmStructVar) == false)
		cout << "Thread 1 Error" <<endl;
	if ( t2.init(&ShmStructVar) == false )
		cout << "Thread 2 Error" <<endl;

	//---------------------------------------------------------- 
	// Start the different threads and runrates
	//---------------------------------------------------------- 
	cout << "Starting threads ... " << endl << flush;
	//if ( t1.start(25.0) != OK )
	if ( t1.start(0.0) != OK )
		cout << "Thread 1 Error" <<endl << flush;
	//if ( t2.start(50.0) != OK )
	if ( t2.start(0.0) != OK )
		cout << "Thread 2 Error" <<endl << flush;
	
	//---------------------------------------------------------- 
	// The main task can still hang out and do stuff
	//---------------------------------------------------------- 
	sleep(3);

	//---------------------------------------------------------- 
	// Shutdown the threads
	//---------------------------------------------------------- 
	cout << "Stopping threads ... " << endl << flush;
	t1.shutdown( );
	t2.shutdown( );

	// give them time
	sleep(1);

	//---------------------------------------------------------- 
	// Total program exit
	//---------------------------------------------------------- 
	exit(0); return 0;	
}
